-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SES-1931] - Fix debouncer crash #1492
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, although there's probably a nice coroutine operator to collect and debounce.
@@ -5,9 +5,9 @@ import android.content.Context | |||
import org.session.libsession.utilities.Debouncer | |||
import org.thoughtcrime.securesms.ApplicationContext | |||
|
|||
class ConversationNotificationDebouncer(private val context: Context) { | |||
class ConversationNotificationDebouncer(private val context: ApplicationContext) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
I'd love to use Kotlin Flow in the hopefully not very far future |
debouncer.publish { publish() } | ||
} | ||
|
||
private fun publish() { | ||
for (threadID in threadIDs.toList()) { | ||
val toNotify = synchronized(threadIDs) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just thinking, you can save the list copy by making it a var
:
private fun publish() {
synchronized(this) {
threadIDs.also { threadIDs = mutableSetOf() }
}.forEach {
context.contentResolver.notifyChange(DatabaseContentProviders.Conversation.getUriForThread(it), null)
}
but it needs the same synchronized(this)
in notify
.
Creating a new instance still needs an allocation, but toList
needs an allocation and an O(n)
copy. Probably negligible performance, so don't feel obliged to change it, but the syntax is nice-ish too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually went down this path to avoid the copying and using AtomicReference
to avoid synchronization but had since decided against it, as I think easier to read code is more important than a few nanoseconds off the hotspot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also considered using a sorted long[]
to avoid boxing but feel really unnecessary here, most of the time you only see a few items in the list really...
Description
The
ConversationNotificationDebouncer
doesn't handle threaded code correctly, added sync block manually.This PR also makes sure no activity context is ever stored inside this singleton object.